package org.origin.ferret.system.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.origin.centre.model.base.XPage;
import org.origin.centre.model.consts.XConst;
import org.origin.centre.model.dto.XDeleteByIdDto;
import org.origin.centre.model.dto.XQueryByIdDto;
import org.origin.centre.model.dto.XUpdateByIdDto;
import org.origin.centre.model.enums.XUsable;
import org.origin.centre.model.vo.XPageVo;
import org.origin.ferret.system.mapper.SysAreaMapper;
import org.origin.ferret.system.mapstruct.SysAreaMapstruct;
import org.origin.ferret.system.model.dto.area.SysAreaInsertDto;
import org.origin.ferret.system.model.dto.area.SysAreaPageDto;
import org.origin.ferret.system.model.dto.area.SysAreaUpdateDto;
import org.origin.ferret.system.model.entity.SysAreaEntity;
import org.origin.ferret.system.model.vo.area.SysAreaVo;
import org.origin.ferret.system.service.SysAreaService;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static org.origin.ferret.system.model.entity.table.SysAreaEntityTableDef.SYS_AREA_ENTITY;

/**
 * 系统区域-服务实现
 *
 * @author ferret
 * @version 2024-05-08
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysAreaServiceImpl implements SysAreaService {
    private final SysAreaMapper areaMapper;
    private final SysAreaMapstruct areaMapstruct;

    @Override
    public XPageVo<SysAreaVo> page(XPage<SysAreaPageDto> pageDto) {
        SysAreaPageDto params = pageDto.getParams();
        if (params.getParentId() == null) params.setParentId(XConst.TOP_ID);
        Page<SysAreaEntity> pagePojo = this.areaMapper
                .paginate(pageDto.getPageNum(), pageDto.getPageSize(), QueryWrapper.create()
                        .where(SYS_AREA_ENTITY.PARENT_ID.eq(params.getParentId()))
                        .and(SYS_AREA_ENTITY.STATUS.eq(params.getStatus(), Objects::nonNull))
                        .and(SYS_AREA_ENTITY.LEVEL.eq(params.getLevel(), Objects::nonNull))
                        .and(SYS_AREA_ENTITY.NAME.like(params.getName(), StringUtils::isNotBlank))
                        .orderBy(SYS_AREA_ENTITY.SORT.asc())
                        .orderBy(SYS_AREA_ENTITY.ID.desc())
                );
        return new XPageVo<SysAreaVo>()
                .of(pageDto, pagePojo.getTotalRow(),
                        pagePojo.getRecords(), this.areaMapstruct::toVo);
    }

    @Override
    public SysAreaVo query(XQueryByIdDto queryDto) {
        SysAreaEntity entity = this.areaMapper.selectOneById(queryDto.getId());
        return this.areaMapstruct.toVo(entity);
    }

    @Override
    public Boolean insert(SysAreaInsertDto insertDto) {
        SysAreaEntity entity = this.areaMapstruct.toEntity(insertDto);
        entity.setStatus(XUsable.USABLE.getCode());
        int insertNum = this.areaMapper.insert(entity);
        return insertNum > 0;
    }

    @Override
    public Boolean update(SysAreaUpdateDto updateDto) {
        SysAreaEntity entity = this.areaMapstruct.toEntity(updateDto);
        entity.setStatus(null);
        int updateNum = this.areaMapper.update(entity);
        return updateNum > 0;
    }

    @Override
    public Boolean delete(XDeleteByIdDto deleteDto) {
        int updateNum = this.areaMapper.deleteById(deleteDto.longId());
        return updateNum > 0;
    }

    @Override
    public Boolean valid(XUpdateByIdDto updateDto) {
        SysAreaEntity entity = new SysAreaEntity();
        entity.setId(updateDto.longId());
        entity.setStatus(XUsable.USABLE.getCode());
        int updateNum = this.areaMapper.update(entity);
        return updateNum > 0;
    }

    @Override
    public Boolean invalid(XUpdateByIdDto updateDto) {
        SysAreaEntity entity = new SysAreaEntity();
        entity.setId(updateDto.longId());
        entity.setStatus(XUsable.DISABLE.getCode());
        int updateNum = this.areaMapper.update(entity);
        return updateNum > 0;
    }

}
